jQuery(document).ready(function($) {
    $("#pguestbook_addform_wrapperbg").insertAfter('body');
    var url = window.location.pathname;
    function refresh() {
    $(function(){
        $('.pguestbook_text').autosize(); 
        $("#pguestbook_textform").val('');
        $("#pguestbook_taskform").val('');
    });
    }
    refresh();
    $(document).on("click","#pguestbook_addform_button", function() {
        $("#pguestbook_addform_wrapperbg").fadeIn(500);
        $('#pguestbook_idform').val('0');
        $('#pguestbook_taskform').val('addpost');
    });
    $(document).on("click",".pguestbook_postwrapper .pguestbook_addc", function() {
        $("#pguestbook_addform_wrapperbg").fadeIn(500);
         $('#pguestbook_idform').val($(this).siblings(".pguestbook_id").val());
        $('#pguestbook_taskform').val('addcomment');
    });
    $(document).on("click","#pguestbook_addform_wrapperbg", function(e) {
        $(this).fadeOut(500);
         e.stopPropagation();
    });
    $(document).on("click","#pguestbook_addform * ", function(e) {
         e.stopPropagation();
    });
    $(document).on("click",".pguestbook_postwrapper .pguestbook_delete", function() {
        $("#pguestbook_postlist").css("opacity","0.5");
        $id = $(this).siblings(".pguestbook_id").val();
        $('#pguestbook_postlist').load(url + " #pguestbook_postlist > * ",{pguestbook_id:$id, pguestbook_task:"deletepost"},function() {$("#pguestbook_postlist").css("opacity","1");refresh();});
    });
    $(document).on("click",".pguestbook_commentwrapper .pguestbook_delete", function() {
        $("#pguestbook_postlist").css("opacity","0.5");
        $id = $(this).siblings(".pguestbook_id").val();
        $('#pguestbook_postlist').load(url + " #pguestbook_postlist > * ",{pguestbook_id:$id, pguestbook_task:"deletecomment"},function() {$("#pguestbook_postlist").css("opacity","1");refresh();});
    });
   $(document).on("click",".pguestbook_commentwrapper .pguestbook_change", function() {
        if ($(this).siblings(".pguestbook_text").is(':disabled')) {
            $(this).siblings(".pguestbook_text").removeAttr('disabled');
            $(this).html('Сохранить');
        } else {
            $("#pguestbook_postlist").css("opacity","0.5");
            $(this).siblings(".pguestbook_text").attr('disabled','disabled');
            $id = $(this).siblings(".pguestbook_id").val();
            $text = $(this).siblings(".pguestbook_text").val();
            $(this).html('Изменить');
            $('#pguestbook_postlist').load(url + " #pguestbook_postlist > * ",{pguestbook_id:$id, pguestbook_task:"changecomment", pguestbook_text:$text},function() {$("#pguestbook_postlist").css("opacity","1");refresh();});
        }
    });
    $(document).on("click",".pguestbook_postwrapper .pguestbook_change", function() {
        if ($(this).siblings(".pguestbook_text").is(':disabled')) {
            $(this).siblings(".pguestbook_text").removeAttr('disabled');
            $(this).html('Сохранить');
        } else {
            $("#pguestbook_postlist").css("opacity","0.5");
            $(this).siblings(".pguestbook_text").attr('disabled','disabled');
            $id = $(this).siblings(".pguestbook_id").val();
            $text = $(this).siblings(".pguestbook_text").val();
            $(this).html('Изменить');
            $('#pguestbook_postlist').load(url + " #pguestbook_postlist > * ",{pguestbook_id:$id, pguestbook_task:"changepost", pguestbook_text:$text},function() {$("#pguestbook_postlist").css("opacity","1");refresh();});
        }
    });
    $(document).on("click","#pguestbook_submit", function(e) {
        $id = $(this).siblings("#pguestbook_idform").val();
        $text = $(this).siblings("#pguestbook_textform").val();
        $autor = $(this).siblings("#pguestbook_autorform").val();
        $task = $(this).siblings("#pguestbook_taskform").val();
        if ($text != '' && $autor != '') {
            $("#pguestbook_postlist").css("opacity","0.5");
            $("#pguestbook_addform_wrapperbg").fadeOut(500);
            $("#pguestbook_form_msg").hide();
            $("#pguestbook_form").css("border","none");
            e.stopPropagation();
            $('#pguestbook_postlist').load(url + " #pguestbook_postlist > * ",{pguestbook_id:$id, pguestbook_task:$task, pguestbook_text:$text, pguestbook_autor:$autor},function() {$("#pguestbook_postlist").css("opacity","1");refresh();});
        } else {
            $("#pguestbook_form_msg").show();
            $("#pguestbook_form").css("border","2px solid red");
        }
    });
});